package com.diyunapp.xilinapp.me;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.diyunapp.xilinapp.R;
import com.diyunapp.xilinapp.bean.SumMoneyBean;
import com.diyunapp.xilinapp.main.MainActivity;
import com.diyunapp.xilinapp.selfdefine.StatusBarFragmentActivity;
import com.diyunapp.xilinapp.utils.AbDateUtil;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

//账户余额
public class SumMoneyActivity extends StatusBarFragmentActivity {

    @Bind(R.id.login_tv)
    TextView rechargeTv;
    @Bind(R.id.register_bn)
    Button getmoneyTv;
    @Bind(R.id.account_money)
    TextView accountMoney;
    @Bind(R.id.freeze_money)
    TextView freezeMoney;
    @Bind(R.id.go_back)
    ImageView goBack;
    @Bind(R.id.money_recorder_list)
    ListView moneyRecorderList;
    @Bind(R.id.notice_refresh_view)
    SwipeRefreshLayout noticeRefreshView;
    private List<SumMoneyBean.ListBean> info=new ArrayList<>();
    private LayoutInflater inflater;
    private MoneyAdapter moneyAdapter;
    private String acountMoney;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sum_money);
        inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
        moneyAdapter =new MoneyAdapter(this);
        ButterKnife.bind(this);
        View headView;
        headView = findViewById(R.id.summoney_menubar_inc);
        super.MenuWidget(headView);
        getData();
        moneyRecorderList.setAdapter(moneyAdapter);
        noticeRefreshView.setColorSchemeColors(R.color.domain_orange, R.color.green,
                R.color.blue, R.color.yellow);

        noticeRefreshView.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                Log.d("zhangxiaoxidebug", "myordersSwiperefreshlayout onRefresh...");
                getData();
            }
        });

    }

    public void getData() {
        String url = ""+MainActivity.url+"act=goods&op=account_balance&username="+ MainActivity.myLogin.getUsername()+"";
        Log.i("zhangxiaoxidebug", "resultinfo=" + url);
        HttpUtils http = new HttpUtils();
        http.configCurrentHttpCacheExpiry(1000);
        http.send(HttpRequest.HttpMethod.POST, url,
                new RequestCallBack<String>() {
                    @Override
                    public void onLoading(long total, long current, boolean isUploading) {}
                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
                        noticeRefreshView.setRefreshing(false);
//                        bidFreshLv.onRefreshComplete();
                        String resultinfo = responseInfo.result;
                        Log.i("zhangxiaoxidebug", "resultinfo=" + resultinfo);
//                        Logger.e("=================================="+resultinfo);
                        try {
                            JSONObject resultObject = new JSONObject(resultinfo);
                            int result = resultObject.optInt("code");
//                            toast(String.valueOf(result));
                            if (result == 200) {
                                info.clear();
                                JSONObject information = resultObject.optJSONObject("date");
                                String available_predeposit=information.optString("available_predeposit");
                                String freeze_predeposit=information.optString("freeze_predeposit");
                                accountMoney.setText(available_predeposit);
                                freezeMoney.setText(freeze_predeposit);
                                JSONArray listArray=resultObject.optJSONArray("list");

                                for (int i = 0; i < listArray.length(); i++) {
                                    JSONObject recordObject = listArray.optJSONObject(i);
                                    String lg_type = recordObject.optString("lg_type");
                                    String lg_av_amount = recordObject.optString("lg_av_amount");
                                    long lg_add_time = recordObject.optLong("lg_add_time");
                                    String lg_desc = recordObject.optString("lg_desc");
                                    String data= lg_type+"++"+lg_av_amount+"++"+lg_add_time+"++"+lg_desc;

                                    info.add(new SumMoneyBean.ListBean(lg_type, lg_av_amount, lg_add_time,lg_desc));
                                    Log.i("zhangxiaoxidebug1111", "info=" + info);

                                }
                                moneyAdapter.setList(info);
                                moneyAdapter.notifyDataSetChanged();
                            } else {
                                toast("当前暂无数据");
                                noticeRefreshView.setVisibility(View.GONE);

                            }
                        } catch (JSONException e) {
                            // TODO Auto-generated catch block
                            toast("查询失败");
                            e.printStackTrace();
                            noticeRefreshView.setVisibility(View.GONE);
                        }
                    }
                    @Override
                    public void onFailure(HttpException error, String msg) {
                        noticeRefreshView.setRefreshing(false);
                        toast("请求失败");
                        noticeRefreshView.setVisibility(View.GONE);
                    }
                });
    }

    @OnClick({R.id.login_tv, R.id.register_bn,R.id.go_back})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.login_tv:
                Intent intent1 = new Intent();
                intent1.setClass(this, RechargeActivity.class);
                intent1.putExtra("accountMoney",accountMoney.getText().toString().trim());
                startActivity(intent1);
                break;
            case R.id.register_bn:
//                nextAct(GetMoneyActivity.class);
                Intent intent2 = new Intent();
                intent2.setClass(this, GetMoneyActivity.class);
                intent2.putExtra("accountMoney",accountMoney.getText().toString().trim());
                startActivity(intent2);
                break;
            case R.id.go_back:
                finish();
                break;
        }
    }



    public void nextAct(Class act) {
        Intent intent = new Intent();
        intent.setClass(this, act);
        startActivity(intent);
    }

    class MoneyAdapter extends BaseAdapter {
        private List<SumMoneyBean.ListBean> info = new ArrayList<SumMoneyBean.ListBean>();
        private Context context;
        public MoneyAdapter(Context context) {
            super();
            this.context = context;
        }

        public void setList(List<SumMoneyBean.ListBean> list) {
            this.info = list;
            notifyDataSetChanged();
        }
        @Override
        public int getCount() {
            return null == info ? 0 : info.size();
        }

        @Override
        public Object getItem(int position) {
            return null == info ? null : info.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }
        private ViewHolder viewHolder;
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            if (convertView == null) {
                convertView = inflater.inflate(R.layout.item_money, null);
                viewHolder = new ViewHolder(convertView);
                convertView.setTag(viewHolder);
                SumMoneyBean.ListBean bean=info.get(position);
                viewHolder.tradeMoney.setText(bean.getLg_av_amount());
                viewHolder.tradeTime.setText(AbDateUtil.getStringByFormat(bean.getLg_add_time()*1000,"yyyy-MM-dd hh:mm:ss"));
                String a = bean.getLg_desc();
                String[] strarray1 = a.split(":");
                String b = strarray1[strarray1.length - 1].toString();
                viewHolder.tradeDanhao.setText(b);
                String type=bean.getLg_type();
                if (type.equals("order_pay")){
                    viewHolder.tradeLeixing.setText("下单支付预存款");
                }else if (type.equals("order_freeze")){
                    viewHolder.tradeLeixing.setText("下单冻结预存款");
                }else if (type.equals("order_cancel")){
                    viewHolder.tradeLeixing.setText("取消订单解冻预存款");
                }else if (type.equals("order_comb_pay")){
                    viewHolder.tradeLeixing.setText("下单支付被冻结的预存款");
                }else if (type.equals("recharge")){
                    viewHolder.tradeLeixing.setText("充值");
                }else if (type.equals("cash_apply")){
                    viewHolder.tradeLeixing.setText("申请提现冻结预存款");
                }else if (type.equals("cash_pay")){
                    viewHolder.tradeLeixing.setText("提现成功");
                }else if (type.equals("cash_del")){
                    viewHolder.tradeLeixing.setText("取消提现申请，解冻预存款");
                }else if (type.equals("refund")){
                    viewHolder.tradeLeixing.setText("退款");
                }

            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }


            return convertView;

        }



        class ViewHolder {
            @Bind(R.id.trade_danhao)
            TextView tradeDanhao;
            @Bind(R.id.trade_time)
            TextView tradeTime;
            @Bind(R.id.trade_money)
            TextView tradeMoney;
            @Bind(R.id.trade_leixing)
            TextView tradeLeixing;

            ViewHolder(View view) {
                ButterKnife.bind(this, view);
            }
        }
    }
}
